package yt.kpool.activity.setting;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.activity.personal.ForgetPwd2Activity;
import yt.kpool.activity.personal.RegisterActivity;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.BaseBean;
import yt.kpool.bean.ChildAccountBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.TimeCountUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.SuccessDialog;

/**
 * 修改密码
 */
public class ChangeActivity extends AppCompatActivity {
    @BindView(R.id.edit_pwd)
    EditText edit_pwd;
    @BindView(R.id.tv_pwd2)
    EditText tv_pwd2;
    @BindView(R.id.tv_vercode)
    EditText tv_vercode;
    @BindView(R.id.tv_verificationcode)
    TextView tv_verificationcode;

    private TimeCountUtil timeCountUtil;//时间计时

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change);
        ButterKnife.bind(this);
        initTitle();
    }

    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("修改密码");
        timeCountUtil = new TimeCountUtil(ChangeActivity.this, 60000, 1000, tv_verificationcode, getResources().getColor(R.color.system_color));
    }

    @OnClick({R.id.btn_next, R.id.tv_verificationcode})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_next://确认
                if (check()) {

                    changePassWord();

                }

                break;
            case R.id.tv_verificationcode:
                //获取验证码
                SPUtil spUtil2 = new SPUtil(BaseApplication.context, BaseApplication.UserLogin);
                String tel = spUtil2.getString(BaseApplication.TEL, "");
                if (!"".equals(tel)) {

                 sendSMS();
                }

                break;
        }
    }

    /**
     * 修改密码
     */
    private void changePassWord() {
        if (NetWorkUtil.check(this)) {


            NetHelperNew.ChangePassWord( getvercode(),getPwd(),new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {


                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                    if (baseBean.getType() == 1) {

                        new SuccessDialog(ChangeActivity.this, "修改成功", new SuccessDialog.OnSuccessListener() {
                            @Override
                            public void getResult() {
                                finish();
                            }
                        }).show();

                    }else {
                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }
    /**
     * 发送验证码
     */
    private void sendSMS() {
        if (NetWorkUtil.check(this)) {

            //倒计时
            timeCountUtil.start();


            NetHelperNew.ChangePassWordSendSmsCode( new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {


                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                    Log.i("-->","修改密码"+baseBean.getContent());
                    if (baseBean.getType() == 1) {


                    }else {
                        ToastUtil.showToast(getApplicationContext(), baseBean.getContent());
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }

    private String getPwd() {
        return edit_pwd.getText().toString().trim();
    }

    private String getPwd2() {
        return tv_pwd2.getText().toString().trim();
    }

    /**
     * 获取验证码
     *
     * @return
     */
    private String getvercode() {
        return tv_vercode.getText().toString().trim();
    }

    private boolean check() {
        if (getPwd().length() == 0) {
            ToastUtil.showToast(getApplicationContext(), "请输入新密码");
            return false;
        }
        if (getPwd2().length() == 0) {
            ToastUtil.showToast(getApplicationContext(), "请输入确认密码");
            return false;
        }
        if (!getPwd().equals(getPwd2())) {
            ToastUtil.showToast(getApplicationContext(), "两次密码不一致");
            return false;
        }
        if (getvercode().length() == 0) {
            ToastUtil.showToast(getApplicationContext(), getString(R.string.error_vercode));
            return false;
        }
        return true;
    }
}
